﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.Client.templates;

namespace ECare.Client.frms
{
    public partial class frmRoleInfo : frmInfo
    {
        public frmRoleInfo()
        {
            InitializeComponent();

            this.Load += frmRoleInfo_Load;
        }

        void frmRoleInfo_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            this.LoadTrees();

            if (this.entity != null)
            {
                this.txtRoleName.Text = (this.entity as RoleInfo).RoleName;
                this.cbActive.Checked = (this.entity as RoleInfo).IsActive;
            }
        }
        public event EntitySavedHandler Saved;
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtRoleName.Text))
            {
                this.errorProvider1.SetError(this.txtRoleName, "empty");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.txtRoleName, "");
            }
            if (this.entity == null)
            {
                var newrole = new RoleInfo();
                newrole.RoleName = this.txtRoleName.Text;
                newrole.IsActive = this.cbActive.Checked;
                newrole.Permissions = this.GetRights();
                var res = RoleInfoHelper.CreateRole(newrole);
                if (res > 0)
                {
                    newrole.RoleId = res;
                    this.entity = newrole;
                }
                else
                {
                    return;
                }
            }
            else
            {
                var editrole = this.entity as RoleInfo;
                editrole.RoleName = this.txtRoleName.Text;
                editrole.IsActive = this.cbActive.Checked;
                editrole.Permissions = this.GetRights();
                RoleInfoHelper.UpldateRole(editrole);
            }
            if (this.Saved != null)
            {
                this.Saved(this.entity);
                this.Close();
            }

        }


        private RolePermissions GetRights()
        {
            var permissions = new RolePermissions();

            foreach (TreeNode node in this.tv.Nodes)
            {
                if (node.Checked)
                {
                    permissions.Modules.Add((int)node.Tag);
                    
                    foreach(TreeNode child in node.Nodes)
                    {
                        if(child.Checked)
                        {
                            permissions.Modules.Add((int)child.Tag);

                            foreach (TreeNode permission in child.Nodes)
                            {
                                if(permission.Checked)
                                {
                                    permissions.permissions.Add((int)permission.Tag);
                                }
                            }

                        }
                    }

                }
            }
            return permissions;
        }

        public List<PermissionInfo> Permissions { get; set; }
        private void LoadTrees()
        {
            var modules = ModulesHelper.GetMenuItems();
            Permissions = PermissionHelper.GetPermissions();

            var TopNodes = modules.Where(a => a.ModuleParentId == -1).ToList();

            foreach (var node in TopNodes)
            {
                var newnode = new TreeNode { Text = node.ModuleName, Tag = node.ModuleId };
                this.tv.Nodes.Add(newnode);

                if (entity != null)
                {
                    var role = entity as RoleInfo;
                    if (role.Permissions.Modules.Contains(node.ModuleId))
                        newnode.Checked = true;
                }


                var children = node.Children;
                foreach (var child in children)
                {
                    var childnode = new TreeNode { Text = child.ModuleName, Tag = child.ModuleId };
                    newnode.Nodes.Add(childnode);


                    if (entity != null)
                    {
                        var role = entity as RoleInfo;
                        if (role.Permissions.Modules.Contains(child.ModuleId))
                            childnode.Checked = true;
                    }

                    LoadPermissions(childnode);
                }
            }
        }

        private void LoadPermissions(TreeNode node)
        {
            var moduleId = (int)node.Tag;
            var rights = this.Permissions.Where(a => a.ModuleId == moduleId).ToList();

            foreach (var right in rights)
            {
                var newnode = new TreeNode();
                newnode.Text = right.PermissionName;
                newnode.Tag = right.PermissionId;
                node.Nodes.Add(newnode);


                if (entity != null)
                {
                    var role = entity as RoleInfo;
                    if (role.Permissions.permissions.Contains(right.PermissionId))
                        newnode.Checked = true;
                }

            }


        }

        private void tv_AfterCheck(object sender, TreeViewEventArgs e)
        {

            if (e.Node.Checked)
            {
                CheckNote(e.Node);
            }else
            {
                UnCheckNode(e.Node);
            }

        }


        private void CheckNote(TreeNode node)
        {
            if (node.Parent != null)
            {
                node.Parent.Checked = true;
                CheckNote(node.Parent);
            }
        }

        private void UnCheckNode(TreeNode node)
        {
            if (node.Nodes != null)
            {
                foreach (TreeNode child in node.Nodes)
                {
                    child.Checked = false;
                    UnCheckNode(child);
                }
            }
        }

    }
}
